#include "measurementbasicsettingform.h"
#include "ui_measurementbasicsettingform.h"
#include <QPainter>
#include <QStyleOption>
#include <QPixmap>
#include <Qt>

MeasurementBasicSettingForm::MeasurementBasicSettingForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MeasurementBasicSettingForm)
{
    ui->setupUi(this);
    this->setProperty("form","backgroundform");
    // 使用样式表设置 toolBox 背景色为白色
    ui->toolBox->setStyleSheet("QWidget { background-color: white; }");
//    ui->label_measurement->setFixedSize()
//    ui->label_measurement->setProperty("model","cs1");
    
    // 连接 toolButton 的 clicked 信号到槽函数
    connect(ui->toolButton, &QToolButton::clicked, this, &MeasurementBasicSettingForm::onToolButtonClicked);
    connect(ui->toolButton_2, &QToolButton::clicked, this, &MeasurementBasicSettingForm::onToolButtonClicked);
    connect(ui->toolButton_3, &QToolButton::clicked, this, &MeasurementBasicSettingForm::onToolButtonClicked);
    connect(ui->toolButton_4, &QToolButton::clicked, this, &MeasurementBasicSettingForm::onToolButtonClicked);
    connect(ui->toolButton_5, &QToolButton::clicked, this, &MeasurementBasicSettingForm::onToolButtonClicked);
    connect(ui->toolButton_6, &QToolButton::clicked, this, &MeasurementBasicSettingForm::onToolButtonClicked);
    connect(ui->toolButton_7, &QToolButton::clicked, this, &MeasurementBasicSettingForm::onToolButtonClicked);
    connect(ui->toolButton_8, &QToolButton::clicked, this, &MeasurementBasicSettingForm::onToolButtonClicked);
    connect(ui->toolButton_9, &QToolButton::clicked, this, &MeasurementBasicSettingForm::onToolButtonClicked);
    connect(ui->toolButton_10, &QToolButton::clicked, this, &MeasurementBasicSettingForm::onToolButtonClicked);
    connect(ui->toolButton_11, &QToolButton::clicked, this, &MeasurementBasicSettingForm::onToolButtonClicked);
    connect(ui->toolButton_12, &QToolButton::clicked, this, &MeasurementBasicSettingForm::onToolButtonClicked);
    connect(ui->toolButton_13, &QToolButton::clicked, this, &MeasurementBasicSettingForm::onToolButtonClicked);
    connect(ui->toolButton_16, &QToolButton::clicked, this, &MeasurementBasicSettingForm::onToolButtonClicked);
    

    // 初始化第一个选中的 toolButton 对应的图片
    updateMeasurementLabel();

//    ui->label_measurement->setPixmap(QPixmap(":/res/MeasurementForm/measurementNoise.png").scaled(36, 36, Qt::KeepAspectRatio, Qt::SmoothTransformation));

}

MeasurementBasicSettingForm::~MeasurementBasicSettingForm()
{
    delete ui;
}

void MeasurementBasicSettingForm::UpdateShowType(int type)
{

    ui->stackedWidget_basicSetting->setCurrentIndex(type);
    ui->stackedWidget_expertMode->setCurrentIndex(type);

}

void MeasurementBasicSettingForm::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.init(this);
    QPainter painter(this);
    style()->drawPrimitive(QStyle::PE_Widget,&opt,&painter,this);
    QWidget::paintEvent(event);
}

void MeasurementBasicSettingForm::onToolButtonClicked()
{
    // 获取触发信号的按钮
    QToolButton *clickedButton = qobject_cast<QToolButton*>(sender());
    if (!clickedButton) return;

    // 取消所有toolButton的选中状态
    QList<QToolButton*> toolButtons = findChildren<QToolButton*>();
    for (QToolButton *button : toolButtons) {
        button->setChecked(false);
    }

    // 确保当前点击的按钮被选中
    clickedButton->setChecked(true);

    // 更新 label_measurement 显示的图片
    updateMeasurementLabel();
}

void MeasurementBasicSettingForm::updateMeasurementLabel()
{
    // 彻底清除当前图片，避免叠加显示
    ui->label_measurement->clear();
    ui->label_measurement->setPixmap(QPixmap()); // 设置空图片
    ui->label_measurement->setText(""); // 清空文本
    ui->label_measurement->setStyleSheet(""); // 清除所有样式表
    ui->label_measurement->update(); // 立即更新控件
    
    // 检查所有 toolButton，找到当前选中的那个
    if (ui->toolButton->isChecked()) {
        // 3D 选中
        ui->label_measurement->setPixmap(QPixmap(":/res/MeasurementForm/3D.png").scaled(36, 36, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    } else if (ui->toolButton_2->isChecked()) {
        // 3D颜色 选中
        ui->label_measurement->setPixmap(QPixmap(":/res/MeasurementForm/3D_color.png").scaled(36, 36, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    } else if (ui->toolButton_3->isChecked()) {
        // 3D自动 选中
        ui->label_measurement->setPixmap(QPixmap(":/res/MeasurementForm/3D_auto.png").scaled(36, 36, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    } else if (ui->toolButton_4->isChecked()) {
        // 图片 选中
        ui->label_measurement->setPixmap(QPixmap(":/res/MeasurementForm/image.png").scaled(36, 36, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    } else if (ui->toolButton_5->isChecked()) {
        // 共聚焦 选中
        ui->label_measurement->setPixmap(QPixmap(":/res/MeasurementForm/confocal.png").scaled(36, 36, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    } else if (ui->toolButton_6->isChecked()) {
        // 坐标 选中
        ui->label_measurement->setPixmap(QPixmap(":/res/MeasurementForm/coordinate.png").scaled(36, 36, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    } else if (ui->toolButton_7->isChecked()) {
        // 3D 选中 (另一个标签页)
        ui->label_measurement->setPixmap(QPixmap(":/res/MeasurementForm/3D.png").scaled(36, 36, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    } else if (ui->toolButton_8->isChecked()) {
        // 测量噪声 选中
        ui->label_measurement->setPixmap(QPixmap(":/res/MeasurementForm/measurementNoise.png").scaled(36, 36, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    } else if (ui->toolButton_9->isChecked()) {
        // 平整度 选中
        ui->label_measurement->setPixmap(QPixmap(":/res/MeasurementForm/flatness.png").scaled(36, 36, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    } else if (ui->toolButton_10->isChecked()) {
        // 放大系数Z 选中
        ui->label_measurement->setPixmap(QPixmap(":/res/MeasurementForm/applificationFactorZ.png").scaled(36, 36, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    } else if (ui->toolButton_11->isChecked()) {
        // PSI 选中 (另一个标签页)
        ui->label_measurement->setPixmap(QPixmap(":/res/MeasurementForm/fringe_PSI.png").scaled(36, 36, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    } else if (ui->toolButton_12->isChecked()) {
        // 图片 选中 (另一个标签页)
        ui->label_measurement->setPixmap(QPixmap(":/res/MeasurementForm/image.png").scaled(36, 36, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    } else if (ui->toolButton_13->isChecked()) {
        // PSI 选中 (另一个标签页)
        ui->label_measurement->setPixmap(QPixmap(":/res/MeasurementForm/fringe_PSI.png").scaled(36, 36, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    } else if (ui->toolButton_16->isChecked()) {
        // CSI 选中
        ui->label_measurement->setPixmap(QPixmap(":/res/MeasurementForm/fringe_CSI.png").scaled(36, 36, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    }
    ui->label_measurement->setScaledContents(true);
}
